<?php
class languages extends CWidget
{
	public function init()
	{
		parent::init();
	}
	public function run()
	{
		$url = $_SERVER['REQUEST_URI'];
		$lang_url = $this->parseUrl($url);
		$curr_language  = Yii::app()->language;
		$Languages = Yii::app()->params['languages'];
		$this->render('default', array('curr_language'=>$curr_language, 'lang_url'	=>	$lang_url));
	}
	public function parseUrl($url)
	{
		$default_language = Yii::app()->params['language_default'];
		$Languages = Yii::app()->params['languages'];
		$langs = array();
		foreach($Languages as $key => $value){
			$langs[$key]['name'] = $value;
			if(strpos($url, '?lang=')!==false){
				$pos = strpos($url, '?');
				$lang = substr($url, $pos+1,7);
				if($default_language==$key)
					$langs[$key]['url'] = str_replace('?'.$lang, '',$url);
					else 
					$langs[$key]['url'] = str_replace($lang, 'lang='.$key, $url);
			}else{
				if($default_language==$key)
					$langs[$key]['url'] = $url;
					else 
					$langs[$key]['url'] = $url.'?lang='.$key;
			}
		}
		return $langs;
	}
}